<?php
namespace App\Http\Controllers\Admin;
use App\Common\Utill\Utills;
use App\Http\Controllers\Controller;
use App\Models\Websites;
use App\Models\WebsitesAdmin;
use App\Models\WebsiteStyle;
use App\Service\AdministratorService;
use App\Service\WebsitesAdminService;
use App\Service\WebsitesService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class WebsitesController extends Controller
{
    /**
     * 展示页面
     * @return
     * */
    public function index()
    {
        $adminId = WebsitesService::getAdminId();
        return view('admin.websites.index')->with('adminId', $adminId);
    }

    /**
     * 首页数据
     * @param Request $request
     * @return mixed
     */
    public function listData(Request $request)
    {
        $num = $request->input('limit',self::ADMIN_PAGE_NUM);
        $server = new WebsitesService();
        $result = $server->getListData($request,$num);
        return Utills::resultData($result);
    }

    /**
     * 获取网站风格
     * @return mixed[]
     */
    public function websiteStyleData()
    {
        $data = WebsiteStyle::query()->where("is_del",1)->select("id","names")->orderBy("id","desc")->get()->toArray();
        return $data;
    }

    /**
     * 添加
     * @return void
     */
    public function createForm()
    {
        $getAdminUserData = AdministratorService::getAdminUserData();
        $websiteStyleData = $this->websiteStyleData();
        return view('admin.websites.create')->with('websiteStyleData',$websiteStyleData)->with('getAdminUserData',$getAdminUserData);
    }

    /**
     * 添加保存
     * @param Request $request
     * @return void
     */
    public function store(Request $request){
        $validator = Validator::make($request->all(), [
            'names' => 'required',
            'domains' => 'required',
        ], [
            'names.required' => '网站名称不能为空',
            'domains.required' => '网站地址不能为空',
        ]);
        if ($validator->fails()) {
            $result = ['code' => 1, 'msg' => $validator->errors()->first()];
            return response()->json($result);
        }
        $names = $request->input('names',"");
        $domains = $request->input('domains',"");
        $emails = $request->input('emails',"");
        $phones = $request->input('phones',"");
        $address = $request->input('address',"");
        $titles = $request->input('titles',"");
        $keywords = $request->input('keywords',"");
        $description = $request->input('description',"");
        $aboutUs = $request->input('about_us',"");
        $image_logo = $request->input('image_logo',"");
        $adminUserId = $request->input('adminUserId',0);
        $websitesStyleId = $request->input('websites_style_id',0);
        $copyright_info = $request->input('copyright_info','');
        $copyright_icp = $request->input('copyright_icp','');
        $server = new WebsitesService();
        $result = $server->store($names,$domains,$titles,$keywords,$description,$image_logo,$adminUserId,$emails,$phones,$address,$aboutUs,$websitesStyleId,$copyright_info,$copyright_icp);
        return Utills::resultData($result);
    }

    /**
     * 网站编辑
     */
    public function editForm(Request $request)
    {
        $id = $request->input('id');
        //获取管理员信息
        $getAdminUserData = AdministratorService::getAdminUserData();
        //获取网站信息
        $data = Websites::query()->where('id',$id)->first();
        $websiteAdmin = WebsitesAdmin::query()->where('website_id',$id)->first();
        $websiteUserId = $websiteAdmin->admin_id;
        $websiteStyleId = $websiteAdmin->websites_style_id;
        //获取登录用户的id
        $adminId = WebsitesAdminService::getAdminId();
        //获取网站风格
        $websiteStyleData = $this->websiteStyleData();
        return view('admin.websites.edit')
            ->with('data',$data)
            ->with('adminId',$adminId)
            ->with('websiteStyleData',$websiteStyleData)
            ->with('getAdminUserData', $getAdminUserData)
            ->with('websiteUserId', $websiteUserId)
            ->with('websiteStyleId', $websiteStyleId);
    }

    public function update(Request $request){
        $validator = Validator::make($request->all(), [
            'names' => 'required',
            'domains' => 'required',
        ], [
            'names.required' => '网站名称不能为空',
            'domains.required' => '网站地址不能为空',
        ]);
        if ($validator->fails()) {
            $result = ['code' => 1, 'msg' => $validator->errors()->first()];
            return response()->json($result);
        }
        $id = $request->input('id',"");
        $names = $request->input('names',"");
        $domains = $request->input('domains',"");
        $emails = $request->input('emails',"");
        $phones = $request->input('phones',"");
        $address = $request->input('address',"");
        $titles = $request->input('titles',"");
        $keywords = $request->input('keywords',"");
        $description = $request->input('description',"");
        $aboutUs = $request->input('about_us',"");
        $image_logo = $request->input('image_logo',"");
        $adminUserId = $request->input('adminUserId',0);
        $websitesStyleId = $request->input('websites_style_id',0);
        $copyright_info = $request->input('copyright_info','');
        $copyright_icp = $request->input('copyright_icp','');
        $server = new WebsitesService();
        $result = $server->update($id,$names,$domains,$titles,$keywords,$description,$image_logo,$adminUserId,$emails,$phones,$address,$aboutUs,$websitesStyleId,$copyright_info,$copyright_icp);
        return Utills::resultData($result);
    }

    /**
     * 状态修改
     * @return void
     */
    public function status(Request $request)
    {
        $validator = \Validator::make($request->all(), [
            'id' => 'required',
        ], [
            'id.required' => 'id不能为空',
        ]);
        if ($validator->fails()) {
            $result = ['code' => 404, 'msg' => $validator->errors()->first()];
            return response()->json($result);
        }
        $id = $request->input('id');
        $server = new WebsitesService();
        $result = $server->status($id);
        return response()->json($result);
    }

    /*
   * 删除
   */
    public function delete(Request $request)
    {
        if (!$request->input('id')) {
            return $this->error('删除失败');
        }
        $id = $request->input('id');
        $server = new WebsitesService();
        $result = $server->delOne($id);
        return Utills::resultData($result);
    }

}
